<template>
    <!-- 表格组件 -->
    <el-table :data="tableData" border>
        <el-table-column width="180" label="日期">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <el-icon>
                        <timer />
                    </el-icon>
                    <span style="margin-left: 10px">{{ scope.row.user_create_time }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column width="90" label="昵称" prop="user_name" />
        <el-table-column width="220" label="邮箱" prop="user_email" />
        <el-table-column width="130" label="电话" prop="user_phone" />
        <el-table-column width="80" label="年龄" prop="user_age" />
        <el-table-column width="80" label="性别" prop="user_sex" />
        <el-table-column label="头像">
            <template #default="scope">
                 <img style="width: 120px; height: 50px;" v-if="scope.row.user_avatar" :src="scope.row.user_avatar" alt="">
            </template>
        </el-table-column>
        <el-table-column width="210" label="地址" prop="user_adress" />
        <el-table-column >
            <template #header>操作</template>
            <template #default="scope">
                <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button size="small" @click="handleDelete(scope.$index, scope.row)" type="danger">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div style="margin-top:20px">
        <el-pagination layout="prev, pager, next" :total="total2" @prev-click="handlePrevChange" @next-click="handleNextChange"  @current-change="handleCurrentChange"/>
    </div>
</template>

<script lang="ts" setup>
// 导入消息提示框
import { ElMessage } from 'element-plus';
import { reactive , ref} from 'vue';
import { userRemoveApi ,userItemsApi , userAllApi} from '../../http/request';
import moment from "moment";
// 导入创建路由方法
import { useRouter } from 'vue-router';
// 定义路由
const $router = useRouter();
// 用户临时身份token
let token = localStorage.getItem('token');
// 定义数据结构
interface User {
    user_create_time: string
    user_name: string
    user_email: string
    user_phone: string
    user_avatar: string
    user_address: string,
    user_age: string,
    user_sex: string,
    user_id: string
}
// 记录数据
let tableData: User[] = reactive([]);
// 交互行为1
const handleEdit = (index: number, row: User) => {
    // console.log('edit',index, row)
    $router.push({path:'/user/publish',query:{user_id: row.user_id}})
}
// 交互行为2
const handleDelete = (index: number, row: User) => {
    // 获取用户id
    let user_id = row.user_id;
    // console.log("图片ID:",user_id);
    // 删除用户和图片
    if(confirm('您确定删除吗')){
        userRemoveApi({ token, user_id})
        .then(
            (data: any) => {
                if (data.code == 200) {
                    tableData.splice(index, 1)
                }
                // 提示
                ElMessage({
                    message: data.msg,
                    type: 'warning',
                });
            }
        )
    }
}
// 初始化用户列表
const initUserList = async (page_num:number,page_size:number) => {
    // 调用用户列表接口
    await userItemsApi({page_num, page_size})
        .then(
            (data: any) => {
                console.log(data);
                if (data.code == 200) {
                    tableData.splice(0,tableData.length);
                    let { result } = data;
                    result.map((item: any) => {
                        if (item.user_create_time) {
                            item.user_create_time = moment(new Date(item.user_create_time)).format('YYYY-MM-DD hh:mm:ss')
                        }
                        tableData.push(item)
                    });
                }
            }
        )
}
// 初始化 
initUserList(1,10);
// 分页处理
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
  initUserList(val,10);
}
const handlePrevChange = (val: number) => {
  console.log(`prev page: ${val}`)
  initUserList(val,10);
}
const handleNextChange = (val: number) => {
  console.log(`next page: ${val}`)
  initUserList(val,10);
}

// 统计用户数量
let total2 = ref (50);
userAllApi()
.then(
    data=>{
       if(data.code == 200){
          //console.log(data)
          total2.value = data.total;
       }
    }
)
</script>